package com.proton.carepatchtemp.utils.net.request;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.apkfuns.logutils.LogUtils;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Settings;
import com.proton.carepatchtemp.utils.NetUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//protontek
public class JGet extends Request implements CodeRequest {
    private final Response.Listener listener;

    public JGet(final String url, List params,
                Response.Listener responseListener) {
        this(url, params, responseListener, Codes.NET_TIMEOUT);
    }

    public JGet(final String url, List params,
                Response.Listener responseListener, final int code) {
        super(Method.GET, NetUtils.getUrlWithParams(url, params), new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                NetUtils.dealVolleyError(volleyError, url, Collections.EMPTY_LIST, code);
            }
        });
        this.listener = responseListener;
        this.setRetryPolicy(new DefaultRetryPolicy(Settings.TIMEOUT_RQS, 0, 1.0f));
        LogUtils.d(url, params);
    }

    @Override
    protected Response parseNetworkResponse(NetworkResponse response) {
        String resultStr = new String(response.data);

        if (resultStr.contains("<!DOCTYPE html>")) {
        }
        if (resultStr.contains("LOGIN") || resultStr.contains("身份校验失败")) {
            NetUtils.dealNeedLogin();
        }

        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            return Response.success(NetUtils.decodeResult(new JSONObject(jsonString)),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        Map<String, String> headers = super.getHeaders();
        if (headers == null
                || headers.equals(Collections.emptyMap())) {
            headers = new HashMap<>();
        }
        VCare.get().addSessionCookie(headers);
        return headers;
    }

    @Override
    protected void deliverResponse(Object rsp) {
        listener.onResponse(rsp);
    }

    @Override
    public Map<String, String> getParams() throws AuthFailureError {
        return new HashMap<String, String>();
    }
}
